/*
 * testSd.c
 *
 *  Created on: 07-11-2013
 *      Author: Kuba
 */
#include "testSd.h"

char str[4]="    ";
char* str_pointer= str;
/*****************************************************************************
 *
 * Description:
 *    Converts integer to string
 *
 ****************************************************************************/
void toString(int*  figure_ptr) {
	int figure = (int)*figure_ptr;

	int i ;
	for (i = 0; i< 3;i++){
		str_pointer[i]=' ';
	}

	if(figure <0 ){
		str_pointer[0]='-';
		figure = - figure;
	}

	if(figure <100){
		int digit= figure %10;
		int ten = figure/10;
		if (ten >0){
			str_pointer[1]= 48+ten;
		}
		str_pointer[2] =48 +digit;
	}
	else{
		int figure2= figure %100;
		int houndred = figure/100;
		int digit= figure2 %10;
		int ten = figure2/10;
		if (houndred >0){
			str_pointer[1]= 48+houndred;
		}
		if (ten >0){
			str_pointer[2]= 48+ten;
		}
		str_pointer[3] =48 +digit;
	}
}

/*****************************************************************************
 *
 * Description:
 *   Saves temperature, depth and boatTilt to file "log.txt" on memory card
 *
 ****************************************************************************/
void saveLog(tS16* temp,int* depth,int * boatTilt){


	WORD bw;
	FRESULT mountrc,openrc,rc=0;

	FATFS fatfs;
	while(1){
		printf("\nMount a volume.\n");
		mountrc = pf_mount(&fatfs);
		if (mountrc){
			printf("Failed to mount with rc=%u.\n", mountrc);
			break;
		}
		else{
			printf("\nOpen a file to write (log.txt).\n");
			openrc = pf_open("log.TXT");
			if (openrc){
				printf("Failed to open with rc=%u.\n", openrc);
				break;
			}
		}

		printf("\nWrite temperature\n");

		rc = pf_write("Temperatura wody wynosi: ", 26, &bw);
		if (rc){
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}
		toString(temp);
		rc = pf_write(str, 4, &bw);
		if (rc) {
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}
		rc = pf_write(" 'C\r\n", 7, &bw);
		if (rc){
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}

		printf("\nWrite depth\n");
		rc = pf_write("Glebokosc wynosi: ", 19, &bw);
		if (rc) {
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}
		toString(depth);
		rc = pf_write(str, 4, &bw);
		if (rc){
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}
		rc = pf_write(" m\r\n", 4, &bw);
		if (rc){
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}

		printf("\nWrite boat tilt\n");
		rc = pf_write("Odchylenie od poziomu: ", 23, &bw);
		if (rc) {
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}
		toString(boatTilt);
		rc = pf_write(str, 4, &bw);
		if (rc){
			printf("Failed to write with rc=%u.\n", rc);
			break;
		}

		printf("\nTerminate the file write process.\n");
		rc = pf_write(0, 0, &bw);
		if (rc){
			printf("Failed to finalize write with rc=%u.\n", rc);
			break;
		}

		break;
	}


}
